package remove;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocal;
import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocalHome;
import org.multimedia.jolitorask.security.AuthServlet;
import org.multimedia.jolitorask.telephone.TelephoneLocal;
import org.multimedia.jolitorask.telephone.TelephoneLocalHome;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;

/**
 * Servlet implementation class for Servlet: RemovePhone
 *
 */
 public class RemovePhone extends AuthServlet implements javax.servlet.Servlet {
		private static final long serialVersionUID = 1L;
		private static final String JNDI_NAME_WRAPPER = "asteriskWrapperSessionLocal";
		private static final String JNDI_NAME = "telephoneLocal";
		
		
		public RemovePhone() {
			super();
		}   	
		
		@SuppressWarnings("unchecked")
		protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

			
			    res.setContentType("text/plain");
			    PrintWriter out = res.getWriter();
			    
			    String utilisateur;
			    String nom;
			    String account;
			    
			    /* On recupere le nom */
			    if( (utilisateur=req.getParameter("login")) == null){
			    	out.println(false);
			    	return;
			    }
			    
			    /* On recupere le nom */
			    if( (nom=req.getParameter("name")) == null){
			    	out.println(false);
			    	return;
			    }
			    
			    /* On recupere le client */
			    if( (account=req.getParameter("account")) == null){
			    	out.println(false);
			    	return;
			    }
			   
			   
			    
			    /* Creation */
			    Context initContext = null;
			    Hashtable env = new Hashtable(1);
			    Object beanObject = null;
			    
			    if( !authentificate(req)){
			    	out.println(false);
			    	return;
			    }
			    	
			    try {
			    	
					Collection<UtilisateurLocal> liste = client.getUtilisateurs();
					
					Iterator<UtilisateurLocal> it = liste.iterator();
					
					UtilisateurLocal user = null;
					while( it.hasNext() ){
						UtilisateurLocal u = it.next();
						
						if(u.getLogin().equals(utilisateur)){
							user = u;
							break;
						}
							
					}
					
					if( user == null){
						out.println(false);
						return;
					}
					
					Iterator<TelephoneLocal> it2 = user.getTelephones().iterator();
					
					TelephoneLocal phone = null;
					while( it2.hasNext() ){
						TelephoneLocal t = it2.next();
						
						if(t.getNom().equals(nom)){
							phone = t;
							break;
						}
							
					}
					
					if( phone == null){
						out.println(false);
						return;
					}
					
					initContext = new javax.naming.InitialContext(env);
			    	beanObject = initContext.lookup(JNDI_NAME);
			    	TelephoneLocalHome home2 = (TelephoneLocalHome) beanObject;
					
			    	int id_telephone = ((Integer)phone.getPrimaryKey()).intValue();
					home2.remove(id_telephone);
					
					//Objet Asterisk
					beanObject = initContext.lookup(JNDI_NAME_WRAPPER);
					AsteriskWrapperLocalHome homeA = (AsteriskWrapperLocalHome)beanObject;
					AsteriskWrapperLocal wrapper = homeA.create();
					wrapper.init(account, utilisateur);
					wrapper.deletePhone(id_telephone);
					
					
					
				}
			    catch(NamingException e2){
			    	out.println(false);
			    	return;
			    }
			    catch (RemoveException e) {
			    	out.println(false);
			    	return;
				}catch (CreateException e) {
			    	out.println(false);
			    	return;
				}
				catch (SQLException e) {
			    	out.println(false);
			    	return;
				}

			    
			    
			    out.println(true);
		}  	
		
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		}   	    
}